<template>
  <div class="app-container">
    <el-form v-show="showSearch" ref="queryForm" :model="queryParams" size="small" :inline="true" label-width="68px">
      <el-form-item label="标题" prop="title">
        <el-input
          v-model="queryParams.title"
          placeholder="请输入标题"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="是否外链" prop="isOutside">
        <el-select v-model="queryParams.isOutside" placeholder="请选择是否外链" clearable>
          <el-option
            v-for="dict in dict.type.banner_is_outside"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="状态" prop="visible">
        <el-select v-model="queryParams.visible" placeholder="请选择状态" clearable>
          <el-option
            v-for="dict in dict.type.common_visible"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="用户名" prop="userName">
        <el-input
          v-model="queryParams.userName"
          placeholder="请输入用户名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          v-hasPermi="['blogs:banner:add']"
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          v-hasPermi="['blogs:banner:edit']"
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          v-hasPermi="['blogs:banner:remove']"
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          v-hasPermi="['blogs:banner:export']"
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
        >导出</el-button>
      </el-col>
      <right-toolbar :show-search.sync="showSearch" @queryTable="getList" />
    </el-row>

    <el-table v-loading="loading" :data="bannerList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="标题" align="center" prop="title" />
      <el-table-column label="banner图" align="center" prop="imgUrl" width="100">
        <template slot-scope="scope">
          <image-preview :src="scope.row.imgUrl" :width="50" :height="50" />
        </template>
      </el-table-column>
      <el-table-column label="是否外链" align="center" prop="isOutside">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.banner_is_outside" :value="scope.row.isOutside" />
        </template>
      </el-table-column>
      <el-table-column label="外链链接" align="center" prop="outsideHref" />
      <el-table-column label="内链博客标题" align="center" prop="note.title" />
      <el-table-column label="排序序号" align="center" prop="orderNum" />
      <el-table-column label="状态" align="center" prop="visible">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.common_visible" :value="scope.row.visible" />
        </template>
      </el-table-column>
      <el-table-column label="用户名" align="center" prop="userName" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            v-hasPermi="['blogs:banner:edit']"
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
          >修改</el-button>
          <el-button
            v-hasPermi="['blogs:banner:remove']"
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改Banner管理对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="标题" prop="title">
          <el-input v-model="form.title" placeholder="请输入标题" />
        </el-form-item>
        <el-form-item label="banner图">
          <image-upload v-model="form.imgUrl" />
        </el-form-item>
        <el-form-item label="是否外链">
          <el-radio-group v-model="form.isOutside">
            <el-radio
              v-for="dict in dict.type.banner_is_outside"
              :key="dict.value"
              :label="parseInt(dict.value)"
            >{{ dict.label }}</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item v-if="form.isOutside === 1" label="外链链接" prop="outsideHref">
          <el-input v-model="form.outsideHref" placeholder="请输入外链链接" />
        </el-form-item>
        <el-form-item v-else label="内链博客" prop="noteId">
          <el-button @click="selectNote">选择博客</el-button>
          <div v-if="form.note && form.note.title">已选择的博客标题: {{ form.note.title }}</div>
        </el-form-item>
        <el-form-item label="排序序号" prop="orderNum">
          <el-input-number v-model="form.orderNum" controls-position="right" :min="0" />
        </el-form-item>
        <el-form-item label="状态">
          <el-radio-group v-model="form.visible">
            <el-radio
              v-for="dict in dict.type.common_visible"
              :key="dict.value"
              :label="parseInt(dict.value)"
            >{{ dict.label }}</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
    <el-dialog title="选择博客" :visible.sync="noteOpen" width="1000px" append-to-body>
      <el-table v-loading="noteLoading" :data="noteList">
        <el-table-column width="50" align="center">
          <template slot-scope="scope">
            <el-radio v-model="noteRow" :label="scope.row">{{ "" }}</el-radio>
          </template>
        </el-table-column>
        <el-table-column label="标题" align="center" prop="title" />
        <el-table-column label="笔记类型" align="center" prop="type">
          <template slot-scope="scope">
            <dict-tag :options="dict.type.bs_note_type" :value="scope.row.type" />
          </template>
        </el-table-column>
        <el-table-column label="浏览量" align="center" prop="pageViewNum" />
        <el-table-column label="点赞数" align="center" prop="likeNum" />
        <el-table-column label="分类组" align="center" prop="groups">
          <template slot-scope="scope">
            <dict-tag :options="groupList" :value="scope.row.groups ? scope.row.groups.split(',').map(Number) : []" />
          </template>
        </el-table-column>
        <el-table-column label="方向" align="center" prop="directionId">
          <template slot-scope="scope">
            <dict-tag :options="directionList" :value="scope.row.directionId ? [scope.row.directionId] : null" />
          </template>
        </el-table-column>
        <el-table-column label="封面图片" align="center" prop="coverPicture" width="100">
          <template slot-scope="scope">
            <image-preview :src="scope.row.coverPicture" :width="50" :height="50" />
          </template>
        </el-table-column>
        <el-table-column label="是否官方推荐" align="center" prop="isOfficialRecommend">
          <template slot-scope="scope">
            <dict-tag :options="dict.type.bs_yes_no" :value="scope.row.isOfficialRecommend" />
          </template>
        </el-table-column>
        <el-table-column label="是否热门" align="center" prop="isHot">
          <template slot-scope="scope">
            <dict-tag :options="dict.type.bs_yes_no" :value="scope.row.isHot" />
          </template>
        </el-table-column>
        <el-table-column label="是否首页" align="center" prop="isHome">
          <template slot-scope="scope">
            <dict-tag :options="dict.type.bs_yes_no" :value="scope.row.isHome" />
          </template>
        </el-table-column>
      </el-table>

      <pagination
        v-show="noteTotal>0"
        :total="noteTotal"
        :page.sync="noteQueryParams.pageNum"
        :limit.sync="noteQueryParams.pageSize"
        @pagination="getNoteList"
      />
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="noteSubmitForm">确 定</el-button>
        <el-button @click="noteOpen = false">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listBanner, getBanner, delBanner, addBanner, updateBanner } from '@/api/blogs/banner'
import { listNote } from '@/api/blogs/note'
import { allListGroup } from '@/api/blogs/group'
import { allListDirection } from '@/api/blogs/direction'
export default {
  name: 'Banner',
  dicts: ['common_visible', 'banner_is_outside', 'bs_yes_no', 'bs_note_type'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 遮罩层
      noteLoading: true,
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // Banner管理表格数据
      bannerList: [],
      // 总条数
      noteTotal: 0,
      // 笔记管理表格数据
      noteList: [],
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      open: false,
      noteOpen: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        title: null,
        isOutside: null,
        visible: null,
        userName: null
      },
      // 查询参数
      noteQueryParams: {
        pageNum: 1,
        pageSize: 10,
        title: null,
        visible: 0,
        isOfficialRecommend: null,
        isHot: null,
        userName: null,
        directionId: null,
        type: null,
        isHome: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        title: [
          { required: true, message: '标题不能为空', trigger: 'blur' }
        ],
        isOutside: [
          { required: true, message: '是否外链不能为空', trigger: 'blur' }
        ],
        orderNum: [
          { required: true, message: '排序序号不能为空', trigger: 'blur' }
        ],
        visible: [
          { required: true, message: '状态不能为空', trigger: 'blur' }
        ],
        outsideHref: [
          { required: true, message: '外链链接不能为空', trigger: 'blur' }
        ],
        noteId: [
          { required: true, message: '内链博客不能为空', trigger: 'blur' }
        ]
      },
      // 分类表格数据
      groupList: [],
      // 方向表格数据
      directionList: [],
      noteRow: null
    }
  },
  created() {
    this.getList()
    this.getAllGroupList()
    this.getAllDireactionList()
  },
  methods: {
    /** 查询Banner管理列表 */
    getList() {
      this.loading = true
      listBanner(this.queryParams).then(response => {
        this.bannerList = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    /** 查询笔记管理列表 */
    getNoteList() {
      this.noteLoading = true
      listNote(this.noteQueryParams).then(response => {
        this.noteList = response.rows
        this.noteTotal = response.total
        this.noteLoading = false
      })
    },
    /** 查询全部分类列表 */
    getAllGroupList() {
      allListGroup().then(response => {
        this.groupList = response.data
      })
    },
    /** 查询全部方向列表 */
    getAllDireactionList() {
      allListDirection().then(response => {
        this.directionList = response.data
      })
    },
    selectNote() {
      this.noteOpen = true
      this.getNoteList()
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        title: null,
        imgUrl: null,
        isOutside: 0,
        outsideHref: null,
        orderNum: null,
        visible: 0,
        userId: null,
        userName: null
      }
      this.resetForm('form')
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm('queryForm')
      this.handleQuery()
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset()
      this.open = true
      this.title = '添加Banner管理'
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset()
      const id = row.id || this.ids
      getBanner(id).then(response => {
        this.form = response.data
        this.open = true
        this.title = '修改Banner管理'
      })
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs['form'].validate(valid => {
        if (valid) {
          this.form.outsideHref = this.form.isOutside == 0 ? null : this.form.outsideHref
          if (this.form.id != null) {
            updateBanner(this.form).then(response => {
              this.$modal.msgSuccess('修改成功')
              this.open = false
              this.getList()
            })
          } else {
            addBanner(this.form).then(response => {
              this.$modal.msgSuccess('新增成功')
              this.open = false
              this.getList()
            })
          }
        }
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids
      this.$modal.confirm('是否确认删除Banner管理编号为"' + ids + '"的数据项？').then(function() {
        return delBanner(ids)
      }).then(() => {
        this.getList()
        this.$modal.msgSuccess('删除成功')
      }).catch(() => {})
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('blogs/banner/export', {
        ...this.queryParams
      }, `banner_${new Date().getTime()}.xlsx`)
    },
    noteSubmitForm() {
      this.form.noteId = this.noteRow.noteId
      this.form.note.title = this.noteRow.title
      this.noteOpen = false
    }
  }
}
</script>
